package com.ant.storage.service.impl;

import com.ant.storage.common.ResultPage;
import com.ant.storage.mapper.BusinessEnterMapper;
import com.ant.storage.mapper.InventoryMapper;
import com.ant.storage.pojo.BusinessEnter;
import com.ant.storage.service.IBusinessEnterService;
import com.ant.storage.service.ICityService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BusinessEnterServiceImpl extends ServiceImpl<BusinessEnterMapper,BusinessEnter> implements IBusinessEnterService{

    @Autowired(required = false)
    BusinessEnterMapper businessEnterMapper;

    @Autowired(required = false)
    InventoryMapper inventoryMapper;

    @Autowired
    ICityService cityService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addBusinessEnter(BusinessEnter businessEnter) throws NoSuchFieldException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        businessEnter.setBusiness_enter_time(simpleDateFormat.format(date));
        String str01 = NullInspectUtil.ClassnullInspect(businessEnter,"business_enter_id","business_enter_remarks","start_time","end_time","city","department","warehouse");
        if(str01 != null){
            NullInspectUtil.myAnnotationError(businessEnter,str01);
        }
        //添加业务入库单
        if(this.save(businessEnter)){
            for (int a = 0;a<businessEnter.getList().size();a++){
                //循环添加业务入库单—商品中间表
                businessEnterMapper.addBusinessEnterCommodity(businessEnter.getBusiness_enter_id(),businessEnter.getList().get(a).getCommodity().getCommodityId(),businessEnter.getList().get(a).getBusiness_enter_number(),businessEnter.getList().get(a).getWarehouse().getId());
                //增加对应库房实际库存
                inventoryMapper.stockAdd(businessEnter.getList().get(a).getCommodity().getCommodityId(),businessEnter.getList().get(a).getBusiness_enter_number(),businessEnter.getList().get(a).getWarehouse().getId());
            }
        }
        return ResultVo.success("ok");
    }

    @Override
    public ResultPage queryBusinessEnter(PageRecv<BusinessEnter> pageRecv) {
        Map map = new HashMap();
        if(!(pageRecv.getData() == null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<BusinessEnter> list = businessEnterMapper.queryBusinessEnter(page,limit,map);
        for (BusinessEnter businessEnter:list) {
            String city = cityService.idQuery(businessEnter.getCity_id());
            String dept = cityService.idQuery(businessEnter.getDepartment_id());
            businessEnter.getCity().setName(city);
            businessEnter.getDepartment().setName(dept);
        }
        Integer count = businessEnterMapper.countQueryBusinessEnter(map);
        return ResultPage.success("ok",list,count);
    }
}
